package com.atguigu.tingshu.account.api;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.login.CustomLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 初始化账户信息（远程调用）
     *
     * @param userId
     * @return
     */
    @GetMapping("/initUserAccount/{userId}")
    public Result initUserAccount(@PathVariable("userId") Long userId) {
        userAccountService.initUserAccount(userId);
        return Result.ok();
    }

    @CustomLogin
    @Operation(summary = "获取用户可用余额")
    @GetMapping("/getAvailableAmount")
    public Result<BigDecimal> getAvailableAmount() {
        BigDecimal availableAmount = userAccountService.getAvailableAmount();
        return Result.ok(availableAmount);
    }

    @CustomLogin
    @Operation(summary = "校验余额并锁定余额")
    @PostMapping("/checkAndLockAccount")
    public Result<AccountLockResultVo> checkAndLockAccount(@RequestBody AccountLockVo accountLockVo) {
        AccountLockResultVo resultVo = userAccountService.checkAndLockAccount(accountLockVo);
        return Result.ok(resultVo);
    }
}

